package cn.com.farsgiht.bluetoothdemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;


import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import cn.com.farsgiht.bluetoothdemo.UI.ChatListViewAdapter;
import cn.com.farsgiht.bluetoothdemo.UI.DrawerHScrollView;
import cn.com.farsgiht.bluetoothdemo.author.jcl;
import cn.com.farsgiht.bluetoothdemo.protocol.SDFileHelper;
import cn.com.farsgiht.bluetoothdemo.protocol.config;
import cn.com.farsgiht.bluetoothdemo.sound.SoundEffect;
import cn.com.farsgiht.bluetoothdemo.task.Task;
import cn.com.farsgiht.bluetoothdemo.task.TaskService;
import cn.com.farsgiht.bluetoothdemo.utils.Utils;

public class ChatActivity extends Activity implements View.OnClickListener{
	private final String TAG = "ChatActivity";
	public static int sAliveCount = 0;
	public static final String EXTRA_MESSAGER = "cn.com.farsgiht.bluetoothdemo.BUNDLE";
	
	// 蓝牙状态变量
	private static int sBTState = -1;
	
	private final int REQUES_BT_ENABLE_CODE = 123;
	private final int REQUES_SELECT_BT_CODE = 222;
	
	private ListView mList;
	private EditText mInput;
	private Button mSendBtn,mBtn_mic;
	private ImageView mEmoButton;
	private GridView mGridView;
	private boolean isUpdate = false;
	//private BluetoothDevice mRemoteDevice;
	//private BluetoothAdapter mBluetoothAdapter;
	
	private LinearLayout mRootLayout, mChatLayout;
	private View mEmoView;
	private boolean isShowEmo = false;
	private int mScrollHeight;
	private SDFileHelper sdFileHelper;
	private DrawerHScrollView mScrollView;
	private ChatListViewAdapter mAdapter2;
	private ArrayList<HashMap<String, Object>> mChatContent2 = new ArrayList<HashMap<String, Object>>();
	private ArrayList<HashMap<String, Object>> mEmoList = new ArrayList<HashMap<String, Object>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat);

		mRootLayout = (LinearLayout) findViewById(R.id.root);
		mChatLayout = (LinearLayout) findViewById(R.id.topPanel);
		mList = (ListView) findViewById(R.id.listView1);
	
		mAdapter2 = new ChatListViewAdapter(this, mChatContent2);
		
		mList.setAdapter(mAdapter2);
		openBtDevice();
		// 初始化表情
		mEmoView = initEmoView();
		
		mInput = (EditText) findViewById(R.id.inputEdit);
		mInput.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 点击输入框后，隐藏表情，显示输入法
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
				imm.showSoftInput(mInput, 0);
				showEmoPanel(false);
			}
		});
		
		mSendBtn = (Button) findViewById(R.id.sendBtn);
		mEmoButton = (ImageView) findViewById(R.id.emotionBtn);
		
		mSendBtn.setOnClickListener(this);
		mEmoButton.setOnClickListener(this);

        //检测读写权限
		mBtn_mic=(Button) findViewById(R.id.btn_mic);
		mBtn_mic.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					myRecorderStare();
					break;
				case MotionEvent.ACTION_UP:
					myRecorderComplete();
					break;
				}
				return false;
			}
		});
        if (isSdCardExist()) {
           config.sdHeplers = new SDFileHelper(getApplicationContext());
        }
         recorder=new MediaRecorder();
         sdFileHelper=new SDFileHelper(this);
	}
    /**
     * 判断SDCard是否存在 [当没有外挂SD卡时，内置ROM也被识别为存在sd卡]
     *
     * @return
     */
	private boolean isSdCardExist() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
	}
	private WifiManager wifiManager;
	private boolean openBtDevice(){
		wifiManager=(WifiManager) getSystemService(Context.WIFI_SERVICE);
		if (wifiManager == null) {
			Toast.makeText(this, "该设备没有wifi设备", Toast.LENGTH_LONG).show();
			return false;//直接返回
		}
		if (!wifiManager.isWifiEnabled()) {
			//wifi光闭
			wifiManager.setWifiEnabled(true);
			startServiceAsServer();
		}else{
			// wifi打开
			startServiceAsServer();
		}

		return true;
		//获取到设备wifi对象

	}

	private View initEmoView(){
		if(mEmoView == null){
			LayoutInflater inflater = getLayoutInflater();
			mEmoView = inflater.inflate(R.layout.emo_layout, null);
			
			mScrollView = (DrawerHScrollView) mEmoView.findViewById(R.id.scrollView);
			mGridView = (GridView) mEmoView.findViewById(R.id.gridView);
			mGridView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
	                // 在android中要显示图片信息，必须使用Bitmap位图的对象来装载  
	                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), (Integer) mEmoList.get(position).get("img")); 
					ImageSpan imageSpan = new ImageSpan(ChatActivity.this, bitmap);  
	                SpannableString spannableString = new SpannableString((String) mEmoList.get(position).get("text"));//face就是图片的前缀名  
	                spannableString.setSpan(imageSpan, 0, 8,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	                mInput.append(spannableString);
	                System.out.println("mInput:"+mInput.getText());
				}
			});

	        mScrollHeight = setScrollGridView(mScrollView, mGridView, 3);
	        System.out.println("mScrollHeight:" + mScrollHeight);
		}
		return mEmoView;
	}
	
	private SimpleAdapter getEmoAdapter(){
		   HashMap<String, Object> map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo001);
		   map.put("text", "<emo001>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo002);
		   map.put("text", "<emo002>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo003);
		   map.put("text", "<emo003>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo004);
		   map.put("text", "<emo004>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo005);
		   map.put("text", "<emo005>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo006);
		   map.put("text", "<emo006>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo007);
		   map.put("text", "<emo007>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo008);
		   map.put("text", "<emo008>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo009);
		   map.put("text", "<emo009>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo010);
		   map.put("text", "<emo010>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo011);
		   map.put("text", "<emo011>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo012);
		   map.put("text", "<emo012>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo013);
		   map.put("text", "<emo013>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo014);
		   map.put("text", "<emo014>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo015);
		   map.put("text", "<emo015>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo016);
		   map.put("text", "<emo016>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo017);
		   map.put("text", "<emo017>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo018);
		   map.put("text", "<emo018>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo019);
		   map.put("text", "<emo019>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo020);
		   map.put("text", "<emo020>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo021);
		   map.put("text", "<emo021>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo022);
		   map.put("text", "<emo022>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo023);
		   map.put("text", "<emo023>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo024);
		   map.put("text", "<emo024>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo025);
		   map.put("text", "<emo025>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo026);
		   map.put("text", "<emo026>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo027);
		   map.put("text", "<emo027>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo028);
		   map.put("text", "<emo028>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo029);
		   map.put("text", "<emo029>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo030);
		   map.put("text", "<emo030>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo031);
		   map.put("text", "<emo031>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo032);
		   map.put("text", "<emo032>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo033);
		   map.put("text", "<emo033>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo034);
		   map.put("text", "<emo034>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo035);
		   map.put("text", "<emo035>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo036);
		   map.put("text", "<emo036>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo037);
		   map.put("text", "<emo037>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo038);
		   map.put("text", "<emo038>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo039);
		   map.put("text", "<emo039>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo040);
		   map.put("text", "<emo040>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo041);
		   map.put("text", "<emo041>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo042);
		   map.put("text", "<emo042>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo043);
		   map.put("text", "<emo043>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo044);
		   map.put("text", "<emo044>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo045);
		   map.put("text", "<emo045>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo046);
		   map.put("text", "<emo046>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo047);
		   map.put("text", "<emo047>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo048);
		   map.put("text", "<emo048>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo049);
		   map.put("text", "<emo049>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo050);
		   map.put("text", "<emo050>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo051);
		   map.put("text", "<emo051>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo052);
		   map.put("text", "<emo052>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo053);
		   map.put("text", "<emo053>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo054);
		   map.put("text", "<emo054>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo055);
		   map.put("text", "<emo055>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo056);
		   map.put("text", "<emo056>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo057);
		   map.put("text", "<emo057>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo058);
		   map.put("text", "<emo058>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo059);
		   map.put("text", "<emo059>");
		   mEmoList.add(map);
		   map = new HashMap<String, Object>();
		   map.put("img", R.drawable.emo060);
		   map.put("text", "<emo060>");
		   mEmoList.add(map);
		   
		   /**
		    * 上述添加表情效率高，但是代码太冗余，下面的方式代码简单，但是效率较低
		    */
		   /*
		   HashMap<String, Integer> map;
		   for(int i = 0; i < 100; i++){
			   map = new HashMap<String, Integer>();
			   Field field=R.drawable.class.getDeclaredField("image"+i);  
			   int resourceId=Integer.parseInt(field.get(null).toString());
			   map.put("img", resourceId);
			   mEmoList.add(map);
		   }
		   */
		return new SimpleAdapter(this, mEmoList, R.layout.grid_view_item, 
					new String[]{"img"}, new int[]{R.id.imageView});
	}
	//作为服务器启动
	private void startServiceAsServer(){
		TaskService.start(this, mHandler);
		TaskService.newTask(new Task(mHandler, Task.TASK_START_ACCEPT, null));
		Log.e("startServiceAsServer", "");
		SoundEffect.getInstance(this).play(SoundEffect.SOUND_PLAY);
	}
	
	@Override
	protected void onResume() {
		sAliveCount++;
		super.onResume();
	}

	@Override
	protected void onPause() {
		sAliveCount--;
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		if (wifiManager.isWifiEnabled()) {
		//	wifiManager.setWifiEnabled(false);
		}
		TaskService.newTask(new Task(mHandler, Task.TASK_END, null));
		TaskService.stop(this);
		appExit();
		super.onDestroy();
	}
	
	
	@Override
	public void onClick(View v) {
		if(v == mSendBtn){
			String msg = mInput.getText().toString().trim();
			TaskService.newTask(new Task(mHandler, Task.TASK_GET_REMOTE_STATE, null));
			if(msg.length() == 0){
				showToast("聊天内容为空");
				SoundEffect.getInstance(ChatActivity.this).play(SoundEffect.SOUND_ERR);
				return;
			}
			
			//------ DEUBG ------ 
			TaskService.newTask(new Task(mHandler, Task.TASK_SEND_MSG, new Object[]{msg}));
			Log.d("mSendBtn", msg);
			showOwnMessage(msg);
			mInput.setText("");
		}else if(v == mEmoButton){
			System.out.println("Emo btn clicked");
			// 关闭输入法
			InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
			imm.hideSoftInputFromWindow(mInput.getWindowToken(),0);
			if(isShowEmo){
				showEmoPanel(false);
			}else{
				showEmoPanel(true);
			}
		}
		
	}
	
	private void showEmoPanel(boolean show){
		if(show && !isShowEmo){
			mEmoView.setVisibility(View.VISIBLE);
			mEmoButton.setImageResource(R.drawable.emo_collapse);
			ViewGroup.LayoutParams params = mChatLayout.getLayoutParams();
			params.height = mChatLayout.getHeight() - mScrollHeight;
			mChatLayout.setLayoutParams(params);
			isShowEmo = true;
		}else if(!show && isShowEmo){
			mEmoView.setVisibility(View.GONE);
			mEmoButton.setImageResource(R.drawable.emo_bkg);
			ViewGroup.LayoutParams params = mChatLayout.getLayoutParams();
			params.height = mChatLayout.getHeight() + mScrollHeight;
			mChatLayout.setLayoutParams(params);
			isShowEmo = false;
		}
		if(!isUpdate && show){
			LinearLayout.LayoutParams para = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			mRootLayout.addView(mEmoView, para);
			isUpdate = true;
		}
	}
	
	// 设置表情的多页滚动显示控件
	public int setScrollGridView(DrawerHScrollView scrollView, GridView gridView, 
			int lines) {
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm); 
		Display display = getWindowManager().getDefaultDisplay();
        System.out.println("Width:" + display.getWidth());
        System.out.println("Height:" + display.getHeight());

		
		int scrollWid = display.getWidth();
		int scrollHei;
		System.out.println("scrollWid:" + scrollWid);
		if (scrollWid <= 0 ){
			Log.d(TAG, "scrollWid or scrollHei is less than 0");
			return 0;
		}
		 
		  
		float density  = dm.density;      // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）
		
		int readlViewWidht = 56;
		// 图片都放在了Hdpi中，所以计算出图片的像素独立宽度
		int viewWidth = (int) (readlViewWidht * density / 1.5);
		int viewHeight = viewWidth;
		System.out.println("viewWidth:" + viewWidth + " viewHeight:" + viewHeight);
		
		int numColsPage = scrollWid / viewWidth;
		int spaceing = (scrollWid - viewWidth * numColsPage)/(numColsPage);
		System.out.println("Space:" + spaceing);


		SimpleAdapter adapter = getEmoAdapter();
		int pages = adapter.getCount() / (numColsPage * lines);
		
		if (pages * numColsPage * lines < adapter.getCount()){
			pages++;
		}

		System.out.println("pages:" + pages);
		
		scrollHei = lines * viewHeight + spaceing * (lines + 1);
		
		LayoutParams params = new LayoutParams(pages * scrollWid, LayoutParams.WRAP_CONTENT);
		gridView.setLayoutParams(params);
		gridView.setColumnWidth(viewWidth);
		gridView.setHorizontalSpacing(spaceing);
		gridView.setVerticalSpacing(spaceing);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setNumColumns(numColsPage * pages);

		//adapter = new DrawerListAdapter(this, colWid, colHei);
		//listener = new DrawerItemClickListener();
		gridView.setAdapter(adapter);
		//mGridView.setOnItemClickListener(listener);

		scrollView.setParameters(pages, 0, scrollWid, spaceing);
		//updateDrawerPageLayout(pageNum, 0);
		// 表情区域还要加上分布显示区
		int pageNumHei = (int) (18 * density); 
		return scrollHei + pageNumHei;
	}

	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			cn.com.farsgiht.bluetoothdemo.protocol.Message message;
			switch(msg.what){
			case -1:
				showToast("没有连接其它用户，点击\"Menu\"扫描并选择周国用户");
				SoundEffect.getInstance(ChatActivity.this).play(SoundEffect.SOUND_ERR);
				break;
			case Task.TASK_RECV_MSG:
				if(msg.obj == null)
					break;
				//消息合法验证
				long time=System.currentTimeMillis();
				if(!timeInspect(time)){	
					
					TaskService.newTask(new Task(mHandler, Task.TASK_END, null));
					//TaskService.newTask(new Task(mHandler, Task.TASK_START_ACCEPT, null));
					break;
				}
				//
				if(msg.obj instanceof HashMap<?, ?>){
					showTargetMessage((HashMap<String, Object>) msg.obj);
				}
				if(sAliveCount <= 0){
					Utils.notifyMessage(ChatActivity.this, "您有未读取消息", ChatActivity.this);
				}
				break;
			case Task.TASK_GET_REMOTE_STATE:
				setTitle((String)msg.obj);
				if(sAliveCount <= 0){
					if(isBTStateChanged(msg.arg1) && msg.arg1 != TaskService.BT_STAT_WAIT)
						Utils.notifyMessage(ChatActivity.this, (String)msg.obj, ChatActivity.this);
				}
				break;
			case Task.TASK_SEND_MSG:
				showToast(msg.obj.toString());
				if(sAliveCount <= 0){
					Utils.notifyMessage(ChatActivity.this, msg.obj.toString(), ChatActivity.this);
				}
				break;
			case Task.TASK_RECV_FILE:
				Bundle b1=msg.getData();
				config.Filetotal=b1.getLong("total");
				config.Filename=b1.getString("fileName");
				sdFileHelper.setFiletotal(b1.getLong("total"));
				try {
					sdFileHelper.savaFileToSD(b1.getString("fileName"));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Log.e("Task.TASK_RECV_FILE", "准备接收文件:"+config.Filename+"文件名大小："+config.Filetotal);
				break;
			case Task.TASK_RECV_FILE_DATA:
			Bundle b=msg.getData();
			String	fileName=b.getString("fileName");
			byte[]  fileData=(byte[])b.getByteArray("fileData");
			int fileDataLength=b.getInt("flieLength");
	
			if (!fileName.equals(config.Filename)) {
				break;
			}else if (config.sise>=config.Filetotal) {
				//文件接收完毕
				try {
					sdFileHelper.OutputClose();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sdFileHelper.setFiletotal(null);
				config.sise=0;
				config.Filename=null;
				config.Filetotal=0;
				System.out.println("接收文件成功");
				Log.e("Task.TASK_RECV_FILE", "接收完成:"+config.Filename+"文件名大小："+config.Filetotal);
			}else {
				try {
					sdFileHelper.savaFileData(fileData);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				config.sise+=fileDataLength;
			} 
				
			
				break;
			}
		}
	};
	
	private boolean isBTStateChanged(int now){
		if(sBTState != now){
			sBTState = now;
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 显示对方信息
	 * @param data
	 */
	private void showTargetMessage(HashMap<String, Object> data){
		SimpleDateFormat df1 = new SimpleDateFormat("E MM月dd日 yy HH:mm ");
		data.put(ChatListViewAdapter.KEY_DATE, df1.format(System.currentTimeMillis()).toString());
		data.put(ChatListViewAdapter.KEY_SHOW_MSG, true);
		mChatContent2.add(data);
		mAdapter2.notifyDataSetChanged();
		SoundEffect.getInstance(ChatActivity.this).play(SoundEffect.SOUND_RECV);
	}
	
	/**
	 * 显示自己信息
	 * @param data
	 */
	private void showOwnMessage(String msg){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put(ChatListViewAdapter.KEY_ROLE, ChatListViewAdapter.ROLE_OWN);
		
		map.put(ChatListViewAdapter.KEY_NAME, "me");
		map.put(ChatListViewAdapter.KEY_TEXT, msg);
		SimpleDateFormat df2 = new SimpleDateFormat("E MM月dd日 yy HH:mm ");
		map.put(ChatListViewAdapter.KEY_DATE, df2.format(System.currentTimeMillis()).toString());
		map.put(ChatListViewAdapter.KEY_SHOW_MSG, true);
		mChatContent2.add(map);
		mAdapter2.notifyDataSetChanged();
		SoundEffect.getInstance(ChatActivity.this).play(SoundEffect.SOUND_SEND);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0,1,0,"重启服务器");
		menu.add(0,2,0,"连接服务器");
		menu.add(0,3,0,"关于作者");
		return super.onCreateOptionsMenu(menu);
	}
	String linkip;
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case 1:
			TaskService.stop(this);
//注意有延迟		
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				startServiceAsServer();
			}
		}).start();	
//			startActivityForResult(new Intent(this, SelectDevice.class), REQUES_SELECT_BT_CODE);
			break;
		case 2:
			AlertDialog.Builder dlg = new AlertDialog.Builder(this);
			final EditText devNameEdit = new EditText(this);
			dlg.setView(devNameEdit);
			dlg.setTitle("请输入服务器ip");
			dlg.setPositiveButton("开始连接", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(devNameEdit.getText().toString().length() != 0)
						//设置你的名字
				//	linkip="192.168.0.5";
					linkip=devNameEdit.getText().toString();
					TaskService.newTask(new Task(mHandler, Task.TASK_START_CONN_THREAD, new Object[]{linkip}));
					}
			});
			dlg.create();
			dlg.show();
			break;
			
		case 3:
/*			String ip="192.168.0.5";
			if (ip==null){
				return true;
			}
			TaskService.newTask(new Task(mHandler, Task.TASK_START_CONN_THREAD, new Object[]{ip}));*/
			//startActivity(new Intent(this, DownloadActivity.class));
			 Intent intent	=new Intent(this,DownloadActivity.class);
		     this.startActivity(intent);
		     //startActivityForResult(new Intent(this, SelectDevice.class), REQUES_SELECT_BT_CODE);
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQUES_BT_ENABLE_CODE && resultCode == RESULT_OK){
			startServiceAsServer();
		}else if(requestCode == REQUES_SELECT_BT_CODE && resultCode == RESULT_OK){
			//mRemoteDevice = data.getParcelableExtra("DEVICE");
			//if(mRemoteDevice == null)
			//	return;
			//TaskService.newTask(new Task(mHandler, Task.TASK_START_CONN_THREAD, new Object[]{mRemoteDevice}));
			String ip="192.168.0.4";
				if (ip==null){
					return;
				}
				TaskService.newTask(new Task(mHandler, Task.TASK_START_CONN_THREAD, new Object[]{ip}));
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void showToast(String msg){
		Toast tst = Toast.makeText(this, msg, Toast.LENGTH_LONG);
		tst.setGravity(Gravity.CENTER | Gravity.TOP, 0, 240);
		tst.show();
	}
	/**
	 * 按两次返回强行退出
	 */
	private long exitTime = 0;
	public boolean onKeyDown(int keyCode, KeyEvent event){  
		  if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){     
		    if((System.currentTimeMillis()-exitTime) > 2000) {//System.currentTimeMillis()无论何时调用，肯定大于2000   
		          Toast.makeText(getApplicationContext(), "再按一次退出程序",Toast.LENGTH_SHORT).show();                                  
	                  exitTime = System.currentTimeMillis();  
	             } else{ 
	            	 TaskService.newTask(new Task(mHandler, Task.TASK_END, null));
	            	    TaskService.stop(this);
		                    finish();  
		                   // appExit();
		                    System.exit(0);//0正常退出1非正常退出 	                    
		                }             
		   return true;  
		                }  
		   return super.onKeyDown(keyCode, event);  
		    }
	
private void appExit(){
	//获得系统当前sdk版本
	int currentVersion = android.os.Build.VERSION.SDK_INT;
    if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) { 
        Intent startMain = new Intent(Intent.ACTION_MAIN); 
        startMain.addCategory(Intent.CATEGORY_HOME); 
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(startMain); 
        System.exit(0); 
    } else {// android2.1 
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
        am.restartPackage(getPackageName()); 
    }
}
	//消息合法验证
 	private boolean msgVerify(String msg, long time) {
 		if (msgInspect(msg)||timeInspect(time)) {
			return true;
		}	
	return false;
 	}
 	//a,b,c验证//msg内容验证
	 String a=null,b=null,c=null;
	 int type=0;
	 int cont=0;
 private boolean msgInspect(String msg) {
	 switch (type) {
	case 0:	
		a=msg;
		type++;
		return true;
	case 1:
		b=a;
		a=msg;
		type++;
		return true;
	case 2:
		c=b;
		b=a;
		a=msg;
		//msg内容对比
		if (a.equals(b)||a.equals(c)) {
			return false;
		}else {
			return true;
		}
	default:
		return false;
	}
}
 long msgTim;
 int typeT=0 ;
 //时间间隔验证
 private boolean timeInspect(long time) {
/*	 if (type==0) {
		msgTim=time;
		return true;
	}else if(time-msgTim>=100) {//消息间隔大于0.1秒为合法
		return true;
	} 
	return false;*/
	switch (typeT) {
	case 0:
		msgTim=time;
		typeT++;
		return true;
	case 1:			
		if(time-msgTim>=100) {//消息间隔大于0.1秒为合法
			msgTim=time;
			return true;
		}else {
			msgTim=time;
			return false;
		}	
	default:
		return false;
	}
}
 
 int START_RECORD = 1;
 //记录初始化标记位
 int RECORD_START = 0;
 //当前状态标记位
 int NOW_STATE = 0;
 String filename;
 MediaRecorder recorder;
 
 private void myRecorderStare() {
	 Log.e("myRecorderStare", "start"); 
         filename = new Date().getTime() + ".amr";
         recorder.reset();
         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         // 从麦克风采集声音
         recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
         // 内容输出格式
         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
         // 音频编码方式

         recorder.setOutputFile("/sdcard/" + filename);// 记住开SD权限
         try {
             recorder.prepare();
         } catch (Exception e) {
             Toast.makeText(this, "请在试一次!", Toast.LENGTH_SHORT).show();
         }// 预期准备
         recorder.start(); // 开始刻录
         RECORD_START = 1;
 }
 
 private void myRecorderComplete() {
  
         recorder.stop();// 停止刻录
         recorder.reset(); // 重设
    
         final String filepath = filename;
         MediaPlayer mediaPlayer = new MediaPlayer();
         if (mediaPlayer.isPlaying()) {
             mediaPlayer.reset();// 重置为初始状态
         }
         try {
             mediaPlayer.setDataSource("/sdcard/" + filepath);
             mediaPlayer.prepare();// 缓冲
             mediaPlayer.start();// 开始或恢复播放
         } catch (Exception e) {
             // TODO: handle exception
         }
         		
         /**
			File ff = new File("D:\\amr", (new Date().getTime()) + "R"
         + ((int) (Math.random() * 100000)) + ".amr");
          */
         //发出去
         File file=new File("/sdcard/" + filename);
         
         TaskService.newTask(new Task(mHandler, Task.TASK_SEND_FILE, new Object[]{(File)file}));
 }
}
